﻿using Iworld.Backstage.Framework.AdministratorService;
using Iworld.Backstage.Framework.GameService;
using System;
using System.Collections.ObjectModel;
using System.Linq;
namespace Iworld.Backstage.Framework
{
    /// <summary>
    /// 牌轮列表视图模型
    /// </summary>
    public class GameChapterListViewModel : ShowListViewModelBase<GameChapterExport, GameServiceClient>
    {
        public DateTime? beginTime = null;
        public DateTime? endTime = null;

        ObservableCollection<FilterBoxModel> statusGroups = new ObservableCollection<FilterBoxModel>();

        public ObservableCollection<FilterBoxModel> StatusGroups
        {
            get { return statusGroups; }
            set
            {
                if (statusGroups == value) { return; }
                statusGroups = value;
                OnPropertyChanged("StatusGroups");
            }
        }
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? BeginTime
        {
            get
            {
                return beginTime;
            }
            set
            {
                if (beginTime != value)
                {
                    beginTime = value;
                    OnPropertyChanged("BeginTime");
                    this.Refresh(1);
                }
            }
        }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime
        {
            get
            {
                return endTime;
            }
            set
            {
                if (endTime != value)
                {
                    endTime = value;
                    OnPropertyChanged("EndTime");
                    this.Refresh(1);
                }
            }
        }

        public GameChapterListViewModel()
            : base(PageCategory.游戏管理.ToString(),Page.牌轮列表.ToString())
        {
            this.client.GetGameChaptersInfoCompleted +=
                this.WrapEventHandler<GetGameChaptersInfoCompletedEventArgs>(GetGameChaptersInfoCompleted);
            StatusGroups.Add(new FilterBoxModel("全部", null, Refresh, true));
            StatusGroups.Add(new FilterBoxModel(GameChapterStatus.从未开奖.ToString(), GameChapterStatus.从未开奖, Refresh));
            StatusGroups.Add(new FilterBoxModel(GameChapterStatus.正在开奖.ToString(), GameChapterStatus.正在开奖, Refresh));
            StatusGroups.Add(new FilterBoxModel(GameChapterStatus.开奖结束.ToString(), GameChapterStatus.开奖结束, Refresh));
            StatusGroups.Add(new FilterBoxModel(GameChapterStatus.终止.ToString(), GameChapterStatus.终止, Refresh));
        }

        void GetGameChaptersInfoCompleted(object sender, GetGameChaptersInfoCompletedEventArgs e)
        {
            if (e.Result.Success)
            {
                this.PageIndex = e.Result.PageIndex;
                this.TotalPage = e.Result.CountOfPage;
                UpdateLsit(e.Result.List);
            }
            else
            {
                ShowError(e.Result.Error);
            }
        }

        protected override void Refresh(object obj)
        {
            int pageIndex = obj == null ? this.PageIndex : Convert.ToInt32(obj);
            client.GetGameChaptersInfoAsync(new GetGameChaptersImport()
            {
                BeginTime = this.BeginTime,
                EndTime = this.EndTime,
                PageIndex = pageIndex,  
                Status = (GameChapterStatus?)this.StatusGroups.First(x=>x.Selected).Value,
                Token = DataManager.GetValue<AdministratorExport>(DataKey.Administrator_Info).Token
            });
        }

        protected override void Reset(object obj)
        {
            this.BeginTime = null;
            this.EndTime = null;
            this.PageIndex = 1;
            Refresh(null);
        }
    }
}
